//
//  PlayMusicManager.swift
//  YYMusicDemo
//
//  Created by yuanyue on 2018/4/27.
//  Copyright © 2018年 yuanyue. All rights reserved.
//

import UIKit


class PlayMusicManager: NSObject {
    static let shareManager = PlayMusicManager()
   
    fileprivate let playMusic = PlayMusic()
    
    var musics : [MusicModel] = [MusicModel]()
    
    var musicDetailModel : MusicDetailModel = MusicDetailModel()
    
    
    fileprivate var currentIndex = -1 {
        didSet {
            if currentIndex < 0 {
                currentIndex = musics.count - 1
            } else if currentIndex > musics.count - 1 {
                currentIndex = 0
            }
        }
    }
    
    func getCurrentMusicDetail() -> MusicDetailModel {
        musicDetailModel.model = musics[currentIndex]
        musicDetailModel.startTime = playMusic.player?.currentTime ?? 0
        musicDetailModel.totalTime = playMusic.player?.duration ?? 0
        musicDetailModel.isPlaying = playMusic.player?.isPlaying ?? false
        
        return musicDetailModel
    }
    
    func breakpointPlay(time : TimeInterval) -> () {
        playMusic.player?.currentTime = time
    }
    
    func playMusic(musicModel : MusicModel) -> () {
        playMusic.playMusic(musicName: musicModel.filename)
        currentIndex = musics.index(of: musicModel)!
    }
    
    //
    func playCurrentMusic() -> () {
        playMusic(musicModel: musics[currentIndex])
    }
    
    func playNextMusic() -> () {
        currentIndex += 1
        playMusic(musicModel: musics[currentIndex])
    }
    
    func playBeginMusic() -> () {
        currentIndex -= 1
        playMusic(musicModel: musics[currentIndex])
    }
    
    func pauseMusic() -> () {
        playMusic.pasueMusic()
    }
    
    func getMusicLyrics(lyricName : String) -> [MusicLrcModel] {
        if lyricName.count <= 0 {
            return [MusicLrcModel]()
        }
        
        guard let path = Bundle.main.path(forResource: lyricName, ofType: nil) else {return [MusicLrcModel]()}
        
        var lyricsContent = ""
        
        do {
            lyricsContent = try String(contentsOfFile: path)
            print(lyricsContent)

        } catch  {
            print(error)
            return [MusicLrcModel]()
        }
        
        let conArray = lyricsContent.components(separatedBy: "\n")
        var lrcArray = [MusicLrcModel]()
        
        for conString in conArray {
            if conString.contains("[ti:") || conString.contains("[ar:") || conString.contains("[al:") {continue}
            
            let oneLineStr = conString.replacingOccurrences(of: "[", with: "")
            
            let timeAndLrcStrArr = oneLineStr.components(separatedBy: "]")
            
            if timeAndLrcStrArr.count != 2 {
                continue
            }
            
            let time = timeAndLrcStrArr[0]
            let lrcStr = timeAndLrcStrArr[1]
            
            let lrcModel = MusicLrcModel()
            lrcModel.beginTime = TimeTool.getFormetString(str: time)
            lrcModel.lrcContent = lrcStr
            lrcArray.append(lrcModel)
   
        }
        
        let count = lrcArray.count
        for i in 0..<count {
            if i == count - 1 {
                continue
            }
            let currentModel = lrcArray[i]
            let nextModel = lrcArray[i + 1]
            currentModel.endTime = nextModel.beginTime
        }
        
        return lrcArray
        
    }

}
